package com.hua.yuan.live;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hua.yuan.common.BaseActivity;
import com.tencent.rtmp.ITXLivePlayListener;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXLivePlayConfig;
import com.tencent.rtmp.TXLivePlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;

/**
 * 直播回看页面
 * @author richsjeson
 */
public class LivePlayActivity extends BaseActivity implements
        SeekBar.OnSeekBarChangeListener,
        ITXLivePlayListener,
        View.OnClickListener
{


    private TXCloudVideoView mPlayVideoView;

    private SeekBar mSbTimeLine;

    private TXLivePlayer mPlayer;

    private TextView tvTimeline;

    private ImageView mImageBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_play_video);
        mPlayVideoView=findViewById(R.id.video_play_live_play);
        mSbTimeLine= findViewById(R.id.sb_live_play);
        tvTimeline=findViewById(R.id.timeline_live_play);
        mImageBack=findViewById(R.id.tv_back_live_play);
        mSbTimeLine.setOnSeekBarChangeListener(this);
        mPlayer = new TXLivePlayer(this);
        mPlayer.setPlayerView(mPlayVideoView);
        mPlayer.setConfig(new TXLivePlayConfig());
        mPlayer.setPlayListener(this);
        mImageBack.setOnClickListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        String url=getIntent().getStringExtra(LiveModule.INTENT_VIDEO_URL);
        mPlayer.startPlay(url, TXLivePlayer.PLAY_TYPE_VOD_FLV);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        mPlayer.seek(seekBar.getProgress());
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onPlayEvent(int event, Bundle param) {

        if (event == TXLiveConstants.PLAY_EVT_PLAY_BEGIN) {
//            stopLoadingAnimation();
        }
        // 如下这段代码是处理播放进度
        else if (event == TXLiveConstants.PLAY_EVT_PLAY_PROGRESS) {
            int progress = param.getInt(TXLiveConstants.EVT_PLAY_PROGRESS);
            int duration = param.getInt(TXLiveConstants.EVT_PLAY_DURATION);

            // UI进度进行相应的调整
            mSbTimeLine.setProgress(progress);
            String durationTime=String.format("%2d:%2d",duration/60,duration%60);
            String processTime=String.format("%2d:%2d",progress/60,progress%60);

            tvTimeline.setText(String.format("%s / %s",processTime,durationTime));
            mSbTimeLine.setMax(duration);
            return;
        }
        // 如下这段代码是处理播放结束的事件
        else if (event == TXLiveConstants.PLAY_ERR_NET_DISCONNECT
                || event == TXLiveConstants.PLAY_EVT_PLAY_END) {

        }
    }

    @Override
    public void onNetStatus(Bundle bundle) {

    }

    @Override
    public void onBackPressed() {
        stopPlay();
        finish();
    }

    /**
     * 停止推流
     */
    private void stopPlay(){
        if(mPlayer==null || !mPlayer.isPlaying()){
            return;
        }
        mPlayer.stopPlay(true);
    }

    @Override
    protected void onDestroy() {
        stopPlay();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {


        if (v.getId() == R.id.tv_back_live_play) {
            onBackPressed();
        }
    }
}
